package com.xiaochong.tir.manager.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.po.Account;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.manager.enums.StatusEnum;
import com.xiaochong.tir.manager.service.AccountService;
import com.xiaochong.tir.manager.util.ManagerVersion;
import com.xiaochong.tir.manager.util.ResultEntity;
import com.xiaochong.tir.manager.util.ReturnResult;

import com.xiaochong.tir.manager.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;



/**
 * 账号管理
 * @author 杨松松
 *
 */
@Controller
@RequestMapping(ManagerVersion.API_V1_PREFIX+"/account/rest")
@Api(value = "账号管理")
public class AccountRestController {

    @Autowired
    private AccountService accountService;
    @Autowired
    private RedisServer redisServer;
    /**
     * 账号列表显示
     *
     * @return
     */
    @ApiOperation(value = "查询账号列表",notes = "查询账号列表")
    @RequestMapping(value = "/accountList")
    @ResponseBody
    public Object videoListJson(@RequestParam(value = "status",required = false) Integer status,
                                                  @RequestParam(value = "find",required = false) String find,
                                                  @RequestParam(value = "startTime", required = false) String startTime,
                                                  @RequestParam(value = "endTime", required = false) String endTime,
                                                  @RequestParam("page") int page,@RequestParam("limit") int limit){
        try {
            PageInfo<Account> pageInfo=  accountService.getAccountList(status, find, startTime, endTime, page, limit);
            return new ReturnResult<Account>(0, "查询成功", pageInfo.getList(), pageInfo.getTotal());
        } catch (Exception e) {
        	e.printStackTrace();
            LogTrace.error(getClass().getSimpleName()+"/accountList", e.getMessage(), e);
        }
        return null;
    }


    /**
     * 更新账号状态
     *
     * @return
     */
    @ApiOperation(value = "修改账号状态",notes = "修改账号状态")
    @GetMapping("/updateStatus")
    @ResponseBody
    public ResultEntity<JSONObject> updateStatus(@RequestParam("id") Integer id,
                                                 @RequestParam("status") Integer status){
        redisServer.set(RedisKeysUtils.LOGINNAME,"更新账号状态");
        ResultEntity<JSONObject> result = new ResultEntity<>();
        try {
            Account account = new Account();
            account.setId(id);
            account.setStatus(status);
            accountService.updateAccount(account,accountService.selectByPrimaryKey(id));
            result.setStatus(StatusEnum.OK);
            //LogTrace.info(getClass().getSimpleName()+"/updateStatus","[back/auth/tag/account/updateStatus]");
        } catch (Exception e) {
            result.setStatus(StatusEnum.SERVER_ERROR);
            LogTrace.error(getClass().getSimpleName()+"/updateStatus", e.getMessage(), e);
        }
        return result;
    }
}
